﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using RG.Common.WPF.Commands;

namespace RG.Common.WPF.Frames
{
    [SupportDataBinding]
    public partial class ErrorsBox 
    {
        public ErrorsBox()
        {
            InitializeComponent();
            ErrorCmd = new DelegateCommand<ValidationErrorMessage>(OnError);
            ErrorCollector = new StringBuilder();
            Messages = new List<ValidationErrorMessage>();
        }

        public string Errors { get; set; }
        public StringBuilder ErrorCollector { get; set; }

        public List<ValidationErrorMessage> Messages { get; set; }

        #region ICommand ErrorCmd
        public static readonly DependencyProperty ErrorProperty =
            DependencyProperty.Register("ErrorCmd", typeof(ICommand), typeof(ErrorsBox),
                                            new FrameworkPropertyMetadata());

        public ICommand ErrorCmd
        {
            set { SetValue(ErrorProperty, value); }
            get { return (ICommand)GetValue(ErrorProperty); }
        }
        #endregion

        #region bool HasErrors
        public static readonly DependencyProperty HasErrorsProperty =
            DependencyProperty.Register("HasErrors", typeof(bool), typeof(ErrorsBox),
                                            new FrameworkPropertyMetadata(false));

        public bool HasErrors
        {
            set { SetValue(HasErrorsProperty, value); }
            get { return (bool)GetValue(HasErrorsProperty); }
        }
        #endregion

        #region StringBuilder OutLineErrors
        public static readonly DependencyProperty OutLineErrorsProperty =
            DependencyProperty.Register("OutLineErrors", typeof(string), typeof(ErrorsBox),
                                            new FrameworkPropertyMetadata("",new PropertyChangedCallback(OutlineErrorsChanged)));

        private static void OutlineErrorsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var temp = (ErrorsBox)d;
            temp.UpdateErrors();
        }

        public string OutLineErrors
        {
            set { SetValue(OutLineErrorsProperty, value);}
            get { return (string)GetValue(OutLineErrorsProperty); }
        }
        #endregion


        private void UpdateErrors()
        {
            var allErrors = new StringBuilder();
            if (ErrorCollector != null && !string.IsNullOrEmpty(ErrorCollector.ToString())) allErrors.Append(ErrorCollector.ToString());
            if (!string.IsNullOrEmpty(OutLineErrors)) allErrors.Append(OutLineErrors);
            Errors = allErrors.ToString();
        }

        private void OnError(ValidationErrorMessage obj)
        {
            if (obj.HasError && Messages.FirstOrDefault(x => x.Sender == obj.Sender) == null &&
                !string.IsNullOrEmpty(obj.ErrorMessage))
                Messages.Add(obj);
            if (!obj.HasError)
            {
                var msg = Messages.FirstOrDefault(x => x.Sender == obj.Sender);
                if (msg != null) Messages.Remove(msg);
            }

            var ec = new StringBuilder();
            foreach (var e in Messages)//erCol)
                ec.AppendLine(!string.IsNullOrEmpty(e.ErrorMessage) ? e.ErrorMessage : "-----------");
            ErrorCollector = ec;

            HasErrors = ErrorCollector.ToString() != "";
            UpdateErrors();
        }

    }
}
